package com.github.lanif.modular.project.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.lanif.core.consts.SymbolConstant;
import com.github.lanif.core.enums.CommonStatusEnum;
import com.github.lanif.core.exception.AuthException;
import com.github.lanif.core.exception.ServiceException;
import com.github.lanif.core.factory.PageFactory;
import com.github.lanif.core.pojo.page.PageResult;
import com.github.lanif.modular.project.entity.Project;
import com.github.lanif.modular.project.mapper.ProjectMapper;
import com.github.lanif.modular.project.param.ProjectParam;
import com.github.lanif.modular.project.result.ProjectResult;
import com.github.lanif.sys.core.context.login.LoginContextHolder;
import com.github.lanif.sys.modular.user.service.SysUserProjectService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author WEI.Q.
 * @date 2021/9/16 11:40
 * @description
 */
@Service
public class ProjectService extends ServiceImpl<ProjectMapper, Project> {
    @Resource
    private SysUserProjectService sysUserProjectService;

    /**
     * 分页查询
     * @param projectParam
     * @return
     */
    public PageResult<Project> page(ProjectParam projectParam) {
        LambdaQueryWrapper<Project> wrapper = new LambdaQueryWrapper();
        if (ObjectUtil.isNotEmpty(projectParam)) {
            if (ObjectUtil.isNotEmpty(projectParam.getName())) {
                wrapper.like(Project::getName, projectParam.getName());
            }
        }
        wrapper.orderByDesc(Project::getCreateTime);
        return new PageResult<>(this.page(PageFactory.defaultPage(), wrapper));
    }

    /**
     * 列表查询
     * @param projectParam
     * @return
     */
    public List<Project> getList(ProjectParam projectParam) {
        LambdaQueryWrapper<Project> wrapper = new LambdaQueryWrapper();
        if (ObjectUtil.isNotEmpty(projectParam)) {
            if (ObjectUtil.isNotEmpty(projectParam.getName())) {
                wrapper.like(Project::getName, projectParam.getName());
            }
        }
        return this.list(wrapper);
    }

    /**
     * 删除
     * @param projectParam
     */
    public void delete(ProjectParam projectParam) {
        this.removeById(projectParam.getId());
    }

    /**
     * 新增
     * @param projectParam
     */
    public void add(ProjectParam projectParam) {
        Project project = new Project();
        BeanUtil.copyProperties(projectParam, project);
        this.save(project);
    }

    /**
     * 修改
     * @param projectParam
     */
    public void edit(ProjectParam projectParam) {
        Project project = this.getById(projectParam.getId());
        BeanUtil.copyProperties(projectParam, project);
        this.updateById(project);
    }
}
